using System;
using System.Net;

namespace SmartSite.Network
{
    // 继承自 EventArgs, 是为了单元测试
    // 单元测试只认非泛型, 符合 .NET Framework 风格的事件
    public class SocketStateEventArgs : EventArgs, ISocketState
    {
        readonly byte[] Buffer;
        public int BufferLength { get => Buffer.Length; }
        public IPEndPoint RemoteEndPoint { get; }
        public IAsyncSocket Socket { get; }

        public SocketStateEventArgs(IAsyncSocket socket, IPEndPoint remoteEndPoint, byte[] buffer)
        {
            Socket = socket;
            RemoteEndPoint = remoteEndPoint;
            Buffer = buffer;
        }

        /// <summary>
        /// 克隆 Buffer
        /// <para>一般用于除去最后CRC<para>
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public byte[] GetBuffer(int count)
        {
            byte[] result = new byte[count];
            for (int i = count - 1; i >= 0; i--) result[i] = Buffer[i];
            return result;
        }

        /// <summary>
        /// 克隆 Buffer
        /// <para>避免直接暴露 Buffer 为属性</para>
        /// </summary>
        /// <returns></returns>
        public byte[] GetBuffer() => GetBuffer(Buffer.Length);
    }
}